package com.example.work4.baiduMapSDK

import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import android.os.Handler
import android.os.Message
import com.baidu.mapapi.map.MapStatusUpdateFactory
import com.baidu.mapapi.map.MyLocationData
import com.baidu.mapapi.model.LatLng
import com.example.work4.fragment.MapFragment
import com.example.work4.po.MyLocation

class LocationListener : BDAbstractLocationListener() {
    var handler:Handler?=null
    override fun onReceiveLocation(location: BDLocation?) {
        if (null != location && location.locType != BDLocation.TypeServerError) {
            val latitude = location.latitude //获取纬度信息
            val longitude = location.longitude //获取经度信息
            val city = location.city //获取城市
            val street = location.street
            val addr = location.locationDescribe.replace("在","").replace("附近","");
            val m_location=MyLocation(longitude,latitude,addr,"$city·$street")

            val m=Message()

            val locData = MyLocationData.Builder()
                .accuracy(location.radius)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()

            MapFragment.baiduMap?.setMyLocationData(locData)

            if(!MapFragment.isCenter) {
                MapFragment.baiduMap?.setMapStatus(
                    MapStatusUpdateFactory.newLatLng(LatLng(latitude, longitude))
                )
                MapFragment.isCenter=true
            }


            if(handler!=null) {
                m.obj = m_location
                m.what = 1
                handler?.sendMessage(m)
            }
        }
    }
}